/**
 * Created by chaoCheng on 2017/1/16.
 */

function FileAddController(Detail,Upload,API,$http,$uibModal,$scope,$state,$stateParams,$filter){
    var vm=this;
    vm.planId=$stateParams.planId;
    vm.planfile = {planId:$stateParams.planId}
    vm.filePath = $stateParams.filePath;
    vm.isSend = false;
    vm.addFile=function(){
        //$("#submit").attr("ng-disable",false)
        vm.isSend = true;
        vm.queryParams = _.clone($stateParams)
        Detail.addFile(vm.planfile).$promise.then(function(response){
            //处理add之后的返回结果
            if(response.success != null) {
                // 模拟窗口打开之后执行该函数
                var modalInstance = $uibModal.open({
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    size: Detail
                });

                // 模拟窗口关闭后回传调用
                modalInstance.result.then(function () {
                    $state.go('detail', {'planId':$stateParams.planId})
                });
                vm.isSend = false;
            } else {
                $scope.serverError = response.fail
                $("#error").show();
                $scope.nofile= response.nofile
            }
        })
    }
    vm.invalid = function(){
        if($("#fileName").val()==""){
            $("#error").hide();
        }else{
            vm.isSend = true;
        }
    }
    vm.cancel = function() {
        $state.go('detail', {'planId':$stateParams.planId})
    }
    $scope.$watch('vm.planfile', function (newValue) {
        console.info(newValue);
        if (newValue) {
            if(newValue.startTime){

                var currentDate = $filter("date")(new Date(), "yyyy-MM-dd");
                var startDate = $filter("date")(new Date(newValue.startTime), "yyyy-MM-dd");

                vm.currentDate = new Date();
                vm.month = vm.currentDate.getMonth() + 1;
                if (vm.month >= 1 &&  vm.month <= 9) {
                    vm.month = "0" +  vm.month;
                }
                vm.dateStr = vm.currentDate.getFullYear()+"-"+vm.month+"-"+vm.currentDate.getDate();
                if(startDate>=currentDate){
                    vm.startTimeInvalid = false
                    vm.form.startTime.$invalid = false
                    vm.isSend = false;
                }else{
                    vm.startTimeInvalid = true
                    vm.form.startTime.$invalid = true
                    vm.form.$invalid = true
                }
            }
            if (newValue.endTime) {
                if (newValue.startTime> newValue.endTime) {
                    vm.timeInvalid = true
                    vm.form.endTime.$invalid = true
                    vm.form.$invalid = true
                } else {
                    vm.timeInvalid = false
                    vm.form.endTime.$invalid = false
                    vm.isSend = false;
                }
    }}},true)

    vm.upload = function(files) {
        if (files && files.length) {
            vm.files = files
            vm.form.$invalid = vm.uploading = true
            _.forEach(files, function(file) {
                vm.file=file;

                Upload.upload({
                    url: API + '/playPlanContoller/upload',
                    data: { 'filename': file.name, 'filelength': file.size },
                    file: file
                }).progress(function(event) {
                        vm.progress = parseInt(100.0 * event.loaded / event.total)
                         $("#noties").html("正在上传......");
                         $("#noties").css("color","red");
                         $("#notie").css("display","block");
                        $("#nofile").css("display","none");
                    })
                    .success(function(response) {
                        var result = response.data;
                        if(result.stat == 'success'){
                            vm.planfile.fileMd5 = response.data.fileMd5;
                            vm.planfile.filePath = response.data.pathUrl;
                            $("#noties").html("上传成功");
                            $("#noties").css("color","green");
                        }else{
                            $("#noties").html(result.msg);
                            $("#noties").css("color","red");
                        }
                        vm.form.$invalid = vm.uploading = false
                    })
                    .error(function(response) {
                        vm.files = undefined
                        vm.form.$invalid = vm.uploading = false
                        debugger
                    })
            })
        }
    }
}
